package surveyBusiness;

import com.alibaba.fastjson.JSONObject;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.GetRequestInputStream;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;
import tools.UUid;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * Created by yangxianming on 2021/6/10.
 */
public class SaveSurvey implements TestServerBusinessImpl {

    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig){
        //读取请求json
        JSONObject requestInfo = GetRequestInputStream.getJson(request);
        ReturnJson returnJson = new ReturnJson();

        //数据库操作
        System.out.println(requestInfo);
        String action = requestInfo.getString("action");

        //变更survey表的type=0，页面将不会展示
        //UPDATE survey SET type=0 WHERE id=13
        if (Objects.equals("editSurvey", action)){
            JSONObject data = requestInfo.getJSONObject("data");
            String sid = data.getString("sid");
            SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
            String sql = "UPDATE survey SET type=0 WHERE id=" + sid;
            System.out.println(sql);
            SqlReturnModel sqlReturnModel = sqlDispatch.update(sql);
            if (sqlReturnModel.getIsSuccess()){
                returnJson.setApiStatus(1);
            }
        }

        //新建条目
        if (Objects.equals("saveSurvey", action)){
            JSONObject data = requestInfo.getJSONObject("data");
            String s_name = data.getString("s_name");
            String s_list = data.getString("s_list");

            SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
            String insertSql = "INSERT INTO `survey` (`surveyname`,`surveycontent`,`type`) VALUES ('"+s_name+"','"+s_list+"','1')";
            System.out.println(insertSql);
            SqlReturnModel sqlReturnModel = sqlDispatch.insert(insertSql);
            if (sqlReturnModel.getIsSuccess()){
                returnJson.setApiStatus(1);
            }
        }
        if (Objects.equals("generateSurvey", action)){
            JSONObject data = requestInfo.getJSONObject("data");
            String surveyId = data.getString("surveyId");
            Integer count = Integer.valueOf(data.getString("count"));

            SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
            String sql = "INSERT INTO surveyuser (`surveyid`,`surveyuniqueid`,`state`) VALUES ";
            StringBuffer values = new StringBuffer();
            for (int i=0; i<count; i++){
                String v = ",("+surveyId+",'"+ UUid.getUUid()+"','0')";
                values.append(v);
            }
            sql = sql + values.deleteCharAt(0);
            System.out.println(sql);
            SqlReturnModel sqlReturnModel = sqlDispatch.insert(sql);
            if (sqlReturnModel.getIsSuccess()){
                returnJson.setApiStatus(1);
            }
        }

        return returnJson.toString();
    }


}
